from zhipuai import ZhipuAI


class GLM4V(object):
    client = ZhipuAI(api_key="47416fa79fbbc3a3adcba19505281bdd.9ECLp1uNc26UUxYS")
    model = "glm-4v-plus"
    prompt =  "注意，如果提问【你看到了什么？】，你要回答，【我看到了xxx】。"

    @staticmethod
    def chat(question, img_base):
        messages=[
            {
              "role": "user", 
              "content": [
                {
                  "type": "image_url",
                  "image_url": {
                    "url" : img_base,
                  }
                },
                {
                  "type": "text",
                  "text": f"{question}。{GLM4V.prompt}"
                }
              ]
            },
        ]
        
        response = GLM4V.client.chat.completions.create(
            model = GLM4V.model,  # 填写需要调用的模型名称
            messages = messages,
            stream=True,
        )
        
        for chunk in response:
            content = chunk.choices[0].delta.content
            yield content
  